import { fileURLToPath, URL } from 'node:url'
import { federation } from '@module-federation/vite'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    cssInjectedByJsPlugin({
      jsAssetsFilterFunction: function customJsAssetsfilterFunction(outputChunk) {
        return outputChunk.fileName == 'remoteEntry.js'
      },
    }),
    federation({
      filename: 'remoteEntry.js',
      name: 'remote-a',
      exposes: {
        './Button': './src/components/Button.vue',
        './BarChart': './src/components/BarChart.vue',
      },
      remotes: {},
      shared: {
        vue: {
          singleton: true,
        },
        echarts: {
          singleton: true,
        },
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  build: {
    target: 'esnext',
    minify: false,
    assetsInlineLimit: 409600,
    rollupOptions: {
      output: {
        minifyInternalExports: true,
      },
    },
  },
  preview: {
    port: 3000,
    cors: true,
  },
})
